 
import Toast from '@vant/weapp/toast/toast';
import Ajax from "../../../utils/ajax"
import Util from "../../../utils/util"
const app = getApp()

Page({
  data: {
    mode: 1,
    cur: 3,
    addr:"",
    tabs: "类型 门牌 面积 特色 价格 照片 状态 描述".split(" "), 
  },
  // 事件处理函数
  changeTab(event) {
    this.setData({cur:event.currentTarget.dataset.id})
  },
  onLoad(options) {
    var th = this;
    if (wx.getUserProfile) {
      if(options.id){
        Ajax.get('/property/getInfoByAuth/' + options.id,{}).then((res)=>{
          console.log("查询成功",res);
          th.methods.handelViewData(res.data)
          th.setData({mode: res.data.saleType,addr:res.data.propertyName}) 
        }); 
      }else{
        Ajax.get(Ajax.api.loadTemp).then(res=>{
          console.log("查询成功",res);
          th.methods.handelViewData(res.data)
          th.setData({mode: res.data.saleType,addr:res.data.propertyName}) 
         })
      }
    }
  }, 
  changeMode (){
    this.setData({ mode: 1 - this.data.mode,})
    app.globalData.tmp.saleType = 1 - this.data.mode
  },
  onChangeAddress(e){
    this.setData({ propertyAddress:e.detail.value })
    console.log(e.detail);
    app.globalData.tmp.propertyName = e.detail.value 
   },
  
  onSave(){  
     Ajax.post(Ajax.api.addTemp,this.methods.handelData()).then(res=>{
      console.log(res);
      Toast(res.msg) 
     })
  },
  onSubmit(){  
    Ajax.post(Ajax.api.property,this.methods.handelData()).then(res=>{
     console.log(res);
     Toast(res.msg) 
    })
 },
  methods: {
    handelViewData(res){
      app.globalData.tmp.cooperationAmount = res.cooperationAmount
      app.globalData.tmp.cooperationProportion = res.cooperationProportion
      app.globalData.tmp.cooperationType = res.cooperationType
      app.globalData.tmp.propertyName = res.propertyName
      app.globalData.tmp.propertyPermission = res.propertyPermission
      app.globalData.tmp.propertyStatus = res.propertyStatus
      app.globalData.tmp.propertyType = res.propertyType
      app.globalData.tmp.saleType = res.saleType
      app.globalData.tmp.slogan =  res.slogan
      app.globalData.tmp.describe =  res.describe
      
      app.globalData.ztApproval = res.ztApproval || {}
      app.globalData.ztFeature = res.ztFeature || {}
      app.globalData.ztLocation = res.ztLocation || {}
      app.globalData.ztPicture = res.ztPicture || {}
      app.globalData.ztPrice = res.ztPrice || {}
      app.globalData.ztSpace = res.ztSpace || {}
    },
    handelData(){
      var data = {  
        masterId:0,
        ztApproval:{  },
        ztFeature:app.globalData.ztFeature,
        ztLocation:app.globalData.ztLocation, 
        ztPicture:app.globalData.ztPicture,
        ztPrice:app.globalData.ztPrice, 
        ztSpace:app.globalData.ztSpace,  
        
       }
       data = Object.assign(data,app.globalData.tmp)
       data.cooperationAmount = parseFloat(data.cooperationAmount)
       data.cooperationProportion = parseFloat(data.cooperationProportion)
       data.cooperationType = parseFloat(data.cooperationType)
       data.propertyStatus = parseFloat(data.propertyStatus)
       data.propertyPermission = data.propertyPermission =='false'?0:1,
  
       data.ztLocation.building = parseFloat( data.ztLocation.building)
       data.ztLocation.certificateType = parseFloat( data.ztLocation.certificateType)
       data.ztLocation.storey = parseFloat( data.ztLocation.storey)
       data.ztLocation.totalStorey = parseFloat( data.ztLocation.totalStorey)
  
       data.ztSpace.balcony = parseFloat( data.ztSpace.balcony)
       data.ztSpace.basement = parseFloat( data.ztSpace.basement)
       data.ztSpace.carport = parseFloat( data.ztSpace.carport)
       data.ztSpace.coveredArea = parseFloat( data.ztSpace.coveredArea)
       data.ztSpace.gardenArea = parseFloat( data.ztSpace.gardenArea)
       data.ztSpace.householdStorey = parseFloat( data.ztSpace.householdStorey)
       data.ztSpace.insideArea = parseFloat( data.ztSpace.insideArea)
       data.ztSpace.interlayer = parseFloat( data.ztSpace.interlayer)
       data.ztSpace.interlayerHeight = parseFloat( data.ztSpace.interlayerHeight)
       data.ztSpace.lift = parseFloat( data.ztSpace.lift)
       data.ztSpace.parlor = parseFloat( data.ztSpace.parlor)
       data.ztSpace.room = parseFloat( data.ztSpace.room)
       data.ztSpace.totalFloor = parseFloat( data.ztSpace.totalFloor)
        
       
       console.log("data",data);
     return data
    },
  },


  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
